package org.masterinftel.scrumpmt;

import java.util.Date;

import org.masterinftel.scrumpmt.activities.ColabActivity;
import org.masterinftel.scrumpmt.activities.estadActivity;
import org.masterinftel.scrumpmt.json.model.ColaboradorJSON;
import org.masterinftel.scrumpmt.json.model.CreateUserResponse;
import org.masterinftel.scrumpmt.json.model.DualListColabJSON;
import org.masterinftel.scrumpmt.json.model.ListColabAll;
import org.masterinftel.scrumpmt.json.model.ProductResponse;
import org.masterinftel.scrumpmt.json.model.ProductoJSON;
import org.masterinftel.scrumpmt.json.model.TareaJSON;
import org.masterinftel.scrumpmt.json.model.TaskResponse;
import org.masterinftel.scrumpmt.json.model.UserRequest;
import org.masterinftel.scrumpmt.json.model.UserResponse;
import org.masterinftel.scrumpmt.protocol.Protocol;
import org.masterinftel.scrumpmt.util.UtilNetwork;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

public class ConsultSessionVar extends Activity {

	private ScrumApplication application;
	private Context ctx;
	private String URLFrontController;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sessionvarview);

		// creamos variables utiles para la aplicacion
		ctx = this;
		application = (ScrumApplication) getApplication();

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		URLFrontController = prefs.getString(Constants.KEY_URL_PREFERENCE, "");

		Button login_button = (Button) findViewById(R.id.LOGIN_BUTTON);
		login_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String urlRest = URLFrontController + "?action="
						+ Protocol.LOGIN;

				UserRequest user = new UserRequest("cristian@gmail.com",
						"cristian");

				JsonReader reader = UtilNetwork.insertJSON(urlRest, user);
				UserResponse respuesta = new Gson().fromJson(reader,
						UserResponse.class);
				// Guardo el sesionID
				application.setSessionID(respuesta.sessionID);
				Log.w(Constants.LOGTAG,
						"LOGIN: " + " - Sesion: " + application.getSessionID());

				valorText.setText("LOGIN: " + " - Sesion: "
						+ application.getSessionID());
			}
		});

		Button l_colab_all_button = (Button) findViewById(R.id.L_COLAB_ALL_BUTTON);
		l_colab_all_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.L_COLAB_ALL;

				JsonReader reader = UtilNetwork.getJSON(urlRest);
				ListColabAll listJSON = new Gson().fromJson(reader,
						ListColabAll.class);

				Log.w(Constants.LOGTAG, "L_COLAB_ALL: " + " - Resultado: ");
				for (ColaboradorJSON c : listJSON.getAllColab())
					Log.i(Constants.LOGTAG, "Colaborador: " + c.getNombre());

				application.setColabAll(listJSON.getAllColab());

				valorText.setText("L_COLAB_ALL");
			}
		});

		Button l_colab_prod_button = (Button) findViewById(R.id.L_COLAB_PROD_BUTTON);
		l_colab_prod_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.L_COLAB_PROD;

				JsonReader reader = UtilNetwork.getJSON(urlRest);
				ListColabAll listJSON = new Gson().fromJson(reader,
						ListColabAll.class);

				Log.w(Constants.LOGTAG, "L_COLAB_PROD: " + " - Resultado: "
						+ listJSON.getAllColab());
				for (ColaboradorJSON c : listJSON.getAllColab())
					Log.i(Constants.LOGTAG, "Colaborador: " + c.getNombre());

				application.setColabProd(listJSON.getAllColab());

				valorText.setText("L_COLAB_PROD");
			}
		});

		Button l_datos_estad_button = (Button) findViewById(R.id.L_DATOS_ESTAD_BUTTON);
		l_datos_estad_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				// int productoID = 233; // Por defecto el primer proyecto que
				// no
				// // esta vacio
				// TextView valorText = (TextView) findViewById(R.id.valor);
				// String urlRest = URLFrontController + ";jsessionid="
				// + application.getSessionID() + "?action="
				// + Protocol.L_DATOS_ESTAD + "&value=" + productoID;
				//
				// JsonReader reader = UtilNetwork.getJSON(urlRest);
				//
				// DatosEstad respuesta = new Gson().fromJson(reader,
				// DatosEstad.class);
				//
				// Log.w(Constants.LOGTAG,
				// "L_DATOS_ESTAD: Producto: " + productoID
				// + " - Resultado: "
				// + respuesta.getBurndownEstimado()[2]);
				//
				// application.setDatosEstad(respuesta);
				//
				// valorText.setText(urlRest);
				startActivity(new Intent(ctx, estadActivity.class));
			}
		});

		Button l_sprint_button = (Button) findViewById(R.id.L_SPRINT_BUTTON);
		l_sprint_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.L_SPRINT + "&value=1";

				JsonReader reader = UtilNetwork.getJSON(urlRest);

				TaskResponse respuesta = new Gson().fromJson(reader,
						TaskResponse.class);
				valorText.setText(new Gson().toJson(respuesta));
			}
		});

		Button l_tareas_all_button = (Button) findViewById(R.id.L_TAREAS_ALL_BUTTON);
		l_tareas_all_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.L_TAREAS_ALL;

				JsonReader reader = UtilNetwork.getJSON(urlRest);

				TaskResponse respuesta = new Gson().fromJson(reader,
						TaskResponse.class);
				valorText.setText(new Gson().toJson(respuesta));
			}
		});

		Button l_colab_pickview_button = (Button) findViewById(R.id.COLAB_PICKLIST);
		l_colab_pickview_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				startActivity(new Intent(ctx, ColabActivity.class));
			}
		});

		Button l_tareas_sprint_button = (Button) findViewById(R.id.L_TAREAS_SPRINT_BUTTON);
		l_tareas_sprint_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.L_TAREAS_SPRINT;

				JsonReader reader = UtilNetwork.getJSON(urlRest);

				Log.w(Constants.LOGTAG, "L_TAREAS_SPRINT: " + " - Sesion: "
						+ application.getSessionID());

				valorText.setText("L_TAREAS_SPRINT");
			}
		});

		Button l_productos_button = (Button) findViewById(R.id.L_PRODUCTOS_BUTTON);
		l_productos_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);

				// Versi�n con preferencias
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.L_PRODUCTOS + "&value=233";

				JsonReader reader = UtilNetwork.getJSON(urlRest);

				ProductResponse respuesta = new Gson().fromJson(reader,
						ProductResponse.class);
				valorText.setText(new Gson().toJson(respuesta));
				Log.w(Constants.LOGTAG, "L_PRODUCTOS: " + " - Valor devuelto: "
						+ new Gson().toJson(respuesta));
			}
		});

		Button add_prod_button = (Button) findViewById(R.id.ADD_PROD);
		add_prod_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				String nombre = "Mi Producto";
				String des = "Es un nuevo producto chulo.";
				String nomcli = "Paco";
				Date fecini = new Date();
				Date fecfin = new Date();
				int horasJornada = 5;
				long scrum_master = 1;
				ProductoJSON new_product = new ProductoJSON(nombre, des,
						nomcli, fecini, fecfin, horasJornada, scrum_master);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.ADD_PROD;

				JsonReader reader = UtilNetwork
						.insertJSON(urlRest, new_product);

				CreateUserResponse respuesta = new Gson().fromJson(reader,
						CreateUserResponse.class);
				valorText.setText(new Gson().toJson(respuesta));
				Log.w(Constants.LOGTAG, "ADD_PROD: " + " - Valor devuelto: "
						+ new Gson().toJson(respuesta));

			}
		});

		Button edit_prod_button = (Button) findViewById(R.id.EDIT_PROD);
		edit_prod_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);
				long id = 233;
				String nombre = "Mi Producto";
				String des = "Es un nuevo producto chulo.";
				String nomcli = "Paco";
				Date fecini = new Date();
				Date fecfin = new Date();
				int horasJornada = 5;
				long scrum_master = 1;
				String scrumMasterNombre = "Agustin";

				ProductoJSON edit_product = new ProductoJSON(id, nombre, des,
						nomcli, fecini, fecfin, horasJornada, scrum_master,
						scrumMasterNombre);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.EDIT_PROD;

				JsonReader reader = UtilNetwork.insertJSON(urlRest,
						edit_product);

				CreateUserResponse respuesta = new Gson().fromJson(reader,
						CreateUserResponse.class);
				valorText.setText(new Gson().toJson(respuesta));
				Log.w(Constants.LOGTAG, "EDIT_PROD: " + " - Valor devuelto: "
						+ new Gson().toJson(respuesta));

			}
		});

		Button add_colab_button = (Button) findViewById(R.id.ADD_COLAB);
		add_colab_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);

				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.ADD_COLAB;

				ColaboradorJSON c = new ColaboradorJSON();

				c.setId(67);
				c.setNombre("Eduardo");
				c.setMail("eduardo@gmail.com");
				c.setAdministrador(true);
				c.setPassword("eduardo");
				c.setProyectoCollection(null);
				c.setProyectoCollection1(null);
				c.setTareaCollection(null);

				JsonReader reader = UtilNetwork.insertJSON(urlRest, c);
				Log.w(Constants.LOGTAG, "ADD_COLAB: " + "Mando: " + c);

			}
		});

		Button edit_colab_button = (Button) findViewById(R.id.EDIT_COLAB);
		edit_colab_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TextView valorText = (TextView) findViewById(R.id.valor);

				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.EDIT_COLAB + "&value=233";

				DualListColabJSON list = new DualListColabJSON();

				// list.setColabAsociados(application.getColabAll());
				// list.setColabNoAsociados(new ArrayList<ColaboradorJSON>());

				JsonReader reader = UtilNetwork.insertJSON(urlRest, list);
				Log.w(Constants.LOGTAG, "EDIT_COLAB: " + "Mando: " + list);

			}
		});

		// Button rem_colab_button = (Button) findViewById(R.id.REM_COLAB);
		// rem_colab_button.setOnClickListener(new OnClickListener() {
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// TextView valorText = (TextView) findViewById(R.id.valor);
		//
		// String urlRest = URLFrontController + ";jsessionid="
		// + application.getSessionID() + "?action="
		// + Protocol.REMOVE_COLAB + "&value=" + 10000;
		//
		// JsonReader reader = UtilNetwork.getJSON(urlRest);
		//
		// }
		// });

		Button remove_prod_button = (Button) findViewById(R.id.REMOVE_PROD);
		remove_prod_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.REMOVE_PROD + "&value=233";

				UtilNetwork.getJSON(urlRest);
			}
		});

		Button change_estad_tarea_button = (Button) findViewById(R.id.CHANGE_ESTAD_TAREA);
		change_estad_tarea_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.CHANGE_ESTAD_TAREA + "&value=6" + "&estad=2";

				UtilNetwork.getJSON(urlRest);
			}
		});

		Button add_tarea_button = (Button) findViewById(R.id.ADD_TAREA);
		add_tarea_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.ADD_TAREA;

				TareaJSON t = new TareaJSON("Nueva tarea de pruebaaaaaaaaaaaa",
						"prueba");

				JsonReader reader = UtilNetwork.insertJSON(urlRest, t);
				Log.w(Constants.LOGTAG, "ADD_COLAB: " + "Mando: " + t);
			}
		});

		Button add_esf_button = (Button) findViewById(R.id.ADD_ESF);
		add_esf_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int idTarea = 2;
				int esf = 8;
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.ADD_ESF + "&value=" + idTarea + "&esf="
						+ esf;

				UtilNetwork.getJSON(urlRest);

				Log.w(Constants.LOGTAG, "ADD_COLAB: " + "Mando: " + urlRest);
			}
		});

		Button add_tarea_sprint_button = (Button) findViewById(R.id.ADD_TAREA_SPRINT);
		add_tarea_sprint_button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int idTarea = 1;
				int idSprint = 2;
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.ADD_TAREA_SPRINT + "&value=" + idTarea
						+ "&sprint=" + idSprint;

				UtilNetwork.getJSON(urlRest);

				Log.w(Constants.LOGTAG, "ADD_ESF: " + "Mando: " + urlRest);
			}
		});
	}
}
